package com.gxuwz.check.dao.admin.impl;

import com.gxuwz.check.base.BaseDao;
import com.gxuwz.check.dao.admin.ITeacherAdminDao;
import com.gxuwz.check.model.dto.TeacherDto;
import com.gxuwz.check.model.entity.SysClassEntity;
import com.gxuwz.check.model.entity.SysTeacherEntity;
import com.gxuwz.check.model.entity.SysUserEntity;
import com.gxuwz.check.util.PageBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Repository("TeacherAdminDaoImpl")
public class TeacherAdminDaoImpl extends BaseDao implements ITeacherAdminDao {
    @Override
    @Transactional
    public List<TeacherDto> list(int page) {
        Criteria criteria = getSession().createCriteria(SysTeacherEntity.class);
        criteria.setFirstResult((page - 1)* new PageBean<>().getPageSize());
        criteria.setMaxResults(new PageBean<>().getPageSize());
        List<SysTeacherEntity> list = criteria.list(); //老师列表
        SessionClose();
        List<TeacherDto> listDto = new ArrayList<>(); //用于封装老师信息
        for (int i = 0 ; i < list.size() ; i ++){
            try {
                TeacherDto teacherDto = new TeacherDto();
                String hql = "select c.collegeName from SysCollegeEntity c where collegeId = ?"; //查出老师的所属学院
                List listCollege = getHibernateTemplate().find(hql,list.get(i).getCollegeId());

                String hql2 = "select u.userName from SysUserEntity u where userId = ?"; //用teacher表里面的user_id去用户表查出老师的账号
                List listUserName = getHibernateTemplate().find(hql2,list.get(i).getUserId());

                teacherDto.setId(i);
                teacherDto.setTeacherId(list.get(i).getTeacherId());
                teacherDto.setUserName((String) listUserName.get(0)); //老师的账号
                if (list.get(i).getTeacherSex() == 1){
                    teacherDto.setTeacherSex("男");
                }else {
                    teacherDto.setTeacherSex("女");
                }
                teacherDto.setTeacherName(list.get(i).getTeacherName());
                teacherDto.setTel(list.get(i).getTeacherTel());
                teacherDto.setCollegeName((String) listCollege.get(0));
                listDto.add(teacherDto);
            }catch (RuntimeException e){
                e.printStackTrace();
            }
        }

        return listDto;
    }

    @Override
    public void delete(Integer teacherId) {
        String sql = "delete from sys_teacher where teacher_id = '"+teacherId+"'";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public void edit() {

    }

    @Override
    @Transactional
    public void save(String teacherName,Integer teacherSex,String teacherTel,String collegeIdAdd,String classId) {
        String sqlUser = "insert into sys_user(user_name,password,role_id) value('"+teacherTel+"',123456,2)";
        //先将教师信息插入用户表
        getSession().createSQLQuery(sqlUser).executeUpdate();
        //插入成功后再取出用户的user_id
        Criteria criteria = getSession().createCriteria(SysUserEntity.class);
        criteria.add(Restrictions.eq("userName",teacherTel));
        List<SysUserEntity> list = criteria.list();
        System.out.println("是否取出数据   "+list);
        //再插入教师详细信息表
        String sqlTeacher = "insert into sys_teacher(teacher_name,teacher_sex,teacher_tel,college_id,class_id,user_id) value('"+teacherName+"','"+teacherSex+"','"+teacherTel+"','"+collegeIdAdd+"','"+classId+"','"+list.get(0).getUserId()+"') ";
        getSession().createSQLQuery(sqlTeacher).executeUpdate();
        SessionClose();
    }

    @Override
    public List<SysClassEntity> listClassByCollegeId(Integer collegeId) {
        Criteria criteria = getSession().createCriteria(SysClassEntity.class);
        criteria.add(Restrictions.eq("collegeId",collegeId));
        List<SysClassEntity> list = criteria.list();
        SessionClose();
        return list;
    }

    @Override
    public SysTeacherEntity findTeacherById(Integer teacherId) {
        Criteria criteria = getSession().createCriteria(SysTeacherEntity.class);
        criteria.add(Restrictions.eq("teacherId",teacherId));
        SysTeacherEntity sysTeacherEntity = (SysTeacherEntity) criteria.list().get(0);
        SessionClose();
        return sysTeacherEntity;
    }
}
